import { _decorator, Color, Component, EventTouch, find, instantiate, JsonAsset, Label, Node, Prefab, Sprite, SpriteFrame, TextAsset, v2, v3 } from 'cc';



import { EventManager } from '../event/EventManager';
import { MapZone } from '../entity/MapZone';

import { VillageInformation } from '../world/VillageInformation';
import ResManager from '../common/ResManager';
import WorldManager from '../common/WorldManager';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { MapObject } from '../world/MapObject';

import { Monster } from '../animal/Monster';
import { TilesManager } from '../common/TilesManager';
import { EffectEnum, EffectManager } from '../common/EffectManager';
import { Village } from '../entity/Village';


const { ccclass, property } = _decorator;

@ccclass('Plane')
export class Plane extends Component {
   
    @property(Label)
    coord: Label;
    @property(Sprite)
    sprite: Sprite
    @property(Sprite)
    zoneType: Sprite
    private x: number; // 场景中真实的坐标 
    private y: number; // 场景中真实的坐标
    public dataX: number; // 显示的坐标
    public dataY: number; // 显示的坐标

    public zone: MapZone; // 地图格子上的数据

    private isClicked: boolean = false
    private touchStartTime: number;

    /**野怪 */
    monster:Node
    /**是否创建过野怪 */
    isCreateMonster:boolean = false
    /**防护罩 */
    guard:Node

    onLoad() {
        
       
    }

    protected onDisable(): void {
        if(this.monster){
            this.monster.destroy()
        }
        if(this.guard){
            this.guard.destroy()
        }
    }

    start() {
        

    }

    update(deltaTime: number) {

    }

    async setZone(zone: MapZone) {
        
        this.zone = zone
        if (zone.typeId > 20) {
            // typeId > 20 表示这里是空地
            let village = Village.createByVillageObj({race:zone.race,people:zone.people})
            try {
                this.zoneType.spriteFrame = await village.getImg()
            } catch (error) {
                
            }
                
            
            

            if (zone.owner) {
                // 空地上有村庄
                if(!this.guard && !zone.isOverThreeDay){
                    this.guard = await EffectManager.instance.play(EffectEnum.Guard, this.node.worldPosition)
                }
            } else {
                // 无人空地
                this.zoneType.node.active = false

                // 生成野怪
                if(!this.isCreateMonster){
                    this.isCreateMonster = true
                    let ran = Math.random()
                    
                    if(ran < 0.1){
                        this.monster =  await Monster.createMonster(PrefabEnum.GoblinKing, this.node.worldPosition)
                    }

                    if(0.1 <= ran && ran < 0.2){
                        this.monster =  await Monster.createMonster(PrefabEnum.BloodKnight, this.node.worldPosition)
                    }
                    
                }
                
            }
        } else {
            // typeId 《 20 表示这里是绿洲
            try {
                this.zoneType.spriteFrame = await zone.getImg()
            } catch (error) {
                
            }
                
            
            
        }

        

    }

    

    setRealPosition(x: number, y: number) {
        this.x = x;
        this.y = y;

        if (!this.coord) {
            this.coord = this.node.getChildByName('coord').getComponent(Label)
        }

        let p = WorldManager.instance.xyToShow(x, y)
        this.dataX = p.x
        this.dataY = p.y


        this.coord.string = this.dataX + ':' + this.dataY;

        this.node.name = this.dataX + ':' + this.dataY;

        this.loadTiles(this.dataX, this.dataY)
    }

    async loadTiles(x:number, y:number){
        try {
            this.sprite.spriteFrame = await TilesManager.getTilesImg(x, y)
        } catch (error) {
            
        }
       
            
        
    }



   

    async onClick() {
        // WorldManager.instance.pickerPlane?.getComponent(Plane).revertColor()
        // WorldManager.instance.pickerPlane = this.node
        // this.sprite.color = new Color('#478000')

        // EventManager.instance.emit(EventManager.EventType.PlaneClick, this.zone)


       
        let ui = await UIManager.instance.openUI(PrefabEnum.VillageInformation)
       
        ui.getComponent(VillageInformation).setData(this.zone)

    }

    revertColor() {

        let c = new Color('#366100')

        this.sprite.color = c
    }


}


